﻿#include "TestActions.h"
#include <QModbusTcpServer>
#include <QHostAddress>


TestActions::TestActions(QObject* parent /*= nullptr*/) {
	m_server = new QModbusTcpServer(parent);
	QModbusDataUnitMap reg;
	reg.insert(QModbusDataUnit::HoldingRegisters, { QModbusDataUnit::HoldingRegisters, 0, 125 });
	m_server->setMap(reg);
	for (int i = 0; i < 512; i++) {
		m_server->setData(QModbusDataUnit::HoldingRegisters, i, 0);
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 0, 1);
	m_server->setConnectionParameter(QModbusDevice::NetworkPortParameter, 502);
	m_server->setConnectionParameter(QModbusDevice::NetworkAddressParameter, "0.0.0.0");
	m_server->setServerAddress(0);
}

TestActions::~TestActions() {

}

void TestActions::connectDevice() {
	if (m_server->connectDevice()) {
		emit logs(QStringLiteral("连接成功"));
	}
	else {
		emit logs(QStringLiteral("连接没有成功，查看端口占用"));
	}
}

void TestActions::disconnectDevice() {
	m_server->disconnectDevice();
	emit logs(QStringLiteral("断开连接"));

}

void TestActions::clearMaps() {
	for (int i = 0; i < 512; i++) {
		m_server->setData(QModbusDataUnit::HoldingRegisters, i, 0);
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 0, 1);
	emit logs(QStringLiteral("清空寄存器"));
}

void TestActions::gripperBackward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 26, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0x00FF;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 26, value);
}

void TestActions::gripperForward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 27, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xFF00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 27, value);
}

void TestActions::gripperLeftward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 19, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0x00FF;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 19, value);
}

void TestActions::gripperRightward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 20, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xFF00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 20, value);
}

void TestActions::gripperStrech(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 19, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xFF00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 19, value);
}

void TestActions::gripperShrink(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 18, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0x00FF;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 18, value);
}

void TestActions::waitPush(bool run) {
	if (run) {
		m_server->setData(QModbusDataUnit::HoldingRegisters, 102, 8);
		emit logs(QStringLiteral("进入到放钻"));
	}
	else {
		m_server->setData(QModbusDataUnit::HoldingRegisters, 102, 0xff);
		emit logs(QStringLiteral("进行抓钻"));
	}
}

void TestActions::pushFrameUpward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 16, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xFF00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 16, value);
}

void TestActions::pushFrameDownward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 15, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0xff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 15, value);
}

void TestActions::pushFrameLeftward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 16, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0xff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 16, value);
}

void TestActions::pushFrameRightward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 17, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xff00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 17, value);
}

void TestActions::palletRightward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 27, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0x00ff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 27, value);
}

void TestActions::palletLeftward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 28, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xff00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 28, value);
}

void TestActions::palletUpward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 10, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0xff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 10, value);
}

void TestActions::palletDownward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 11, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xff00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 11, value);
}

void TestActions::pushDrillrod(bool run) {
	if (run) {
		m_server->setData(QModbusDataUnit::HoldingRegisters, 101, 5);
	}
	else {
		m_server->setData(QModbusDataUnit::HoldingRegisters, 101, 0xff);
		m_server->setData(QModbusDataUnit::HoldingRegisters, 102, 4);
	}
}

void TestActions::pushDrill(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 32, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0x00ff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 32, value);
}

void TestActions::pullDrill(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 33, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xff00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 33, value);
}

void TestActions::drillFrameUnwind(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 25, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0xff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 25, value);
}

void TestActions::drillFrameShrink(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 26, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xff00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 26, value);
}

void TestActions::drillFrameUpward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 25, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xff00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 25, value);
}

void TestActions::drillFrameDownward(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 24, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0xff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 24, value);
}

void TestActions::frontFolderClose(bool run) {
	quint16 value = 0;




	m_server->data(QModbusDataUnit::HoldingRegisters, 22, &value);

	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0xff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 22, value);
}

void TestActions::frontFolderOpen(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 23, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xff00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 23, value);
}

void TestActions::backFolderClose(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 21, &value);
	if (run) {
		value = value | 0x6400;
	}
	else {
		value = value & 0xff;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 21, value);
}

void TestActions::backFolderOpen(bool run) {
	quint16 value = 0;
	m_server->data(QModbusDataUnit::HoldingRegisters, 22, &value);
	if (run) {
		value = value | 0x64;
	}
	else {
		value = value & 0xff00;
	}
	m_server->setData(QModbusDataUnit::HoldingRegisters, 22, value);
}

void TestActions::startGripper(bool run) {
	if (run) {
		m_server->setData(QModbusDataUnit::HoldingRegisters, 102, 7);
	}
	else {
		m_server->setData(QModbusDataUnit::HoldingRegisters, 102, 8);
	}
}

